package com.heima.seckill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.seckill.mapper.SeckillOrderMapper;
import com.heima.seckill.pojo.SeckillOrder;
import com.heima.seckill.pojo.User;
import com.heima.seckill.service.ISeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 秒杀订单表 服务实现类
 * </p>
 *
 * @author zhujia
 * @since 2022-04-20
 */
@Service
public class SeckillOrderServiceImpl extends ServiceImpl<SeckillOrderMapper, SeckillOrder> implements ISeckillOrderService {
    @Autowired
    private SeckillOrderMapper seckillOrderMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     *  获取秒杀结果
     * @return orderId 秒杀成功 -1 秒杀失败  0 排队中
     */
    @Override
    public Long getResult(User user, String goodsId) {
        SeckillOrder seckillOrder = seckillOrderMapper .selectOne (new QueryWrapper<SeckillOrder>() .eq("user_id",
                user.getId()).eq( "goods_id",goodsId));
        if (null != seckillOrder) {
            return seckillOrder.getOrderId();
        } else if (redisTemplate.hasKey("isStockEmpty:" + goodsId)) {
            return -1L;
        } else{
            return 0L;
    }


}
}
